package C037;

/**
 * ClassName: C09
 * Package: C037
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/23 20:57
 * @Version 1.0
 * @Since 1.0
 */
public class C09 {
    public static void main(String[] args) {

    }

    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }


    static int yes, no;
    // 打家劫舍问题
    public static int rob(TreeNode root) {
        if(root == null) {
            return 0;
        }

        process(root);
        return Math.max(yes, no);
    }


    private static void process(TreeNode root) {
        if(root == null){
            yes = 0;
            no = 0;
        }else {
            int y = root.val;
            int n = 0;
            process(root.left);
            y += no;
            n += Math.max(yes, no);
            process(root.right);
            y += no;
            n += Math.max(yes, no);
            yes = y;
            no = n;
        }
    }
}
